package com.ilink.teacherservice.service.impl.xiaolian;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ilink.teacherservice.entity.PO.xiaolian.XiaolianConversationLogs;
import com.ilink.teacherservice.mapper.xiaolian.XiaolianConversationLogsMapper;
import com.ilink.teacherservice.service.xiaolian.RecordConversationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import lombok.extern.slf4j.Slf4j;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.util.Date;

@Service
@Slf4j
@Transactional
public class RecordConversationServiceImpl implements RecordConversationService {

    @Autowired
    private XiaolianConversationLogsMapper xiaolianConversationLogsMapper;


    /**
     * @return boolean
     * @Description 向对话记录表插入一条新的对话信息
     * @Param userId 用户 ID, courseId 课程 ID, userInput 用户输入, assistantReply 智能助手回复
     **/
    @Override
    public boolean insertConversationLog(String userId, String CourseFilePath, String userInput, String assistantReply, String assistantReference) {
        try {

            XiaolianConversationLogs conversationLog = new XiaolianConversationLogs();
            conversationLog.setUserId(userId);
            conversationLog.setCourseFilePath(CourseFilePath);
            conversationLog.setUserInput(userInput);
            conversationLog.setAssistantReply(assistantReply);
            conversationLog.setAssistantReference(assistantReference);
            // 获取当前时间并精确到分钟
            LocalDateTime now = LocalDateTime.now().withSecond(0).withNano(0); // 去掉秒和毫秒部分
            Date currentTime = Date.from(now.atZone(ZoneId.systemDefault()).toInstant()); // 转换为 Date 类型
            conversationLog.setAskTime(currentTime); // 设置时间字段
            log.info("当前时间 (精确到分钟): {}", currentTime); // 打印当前时间以验证
            xiaolianConversationLogsMapper.insert(conversationLog);
            return true;
        } catch (Exception e) {
            log.error("RecordConversationServiceImpl插入对话记录失败", e);
            return false;
        }
    }
}
